package com.higgins.eventtracking.client.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.ChartDataDTO;
import com.higgins.eventtracking.client.dto.FinanceGoalDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;
import com.higgins.eventtracking.server.GenericDataStoreService;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.corechart.AxisOptions;
import com.google.gwt.visualization.client.visualizations.corechart.BarChart;
import com.google.gwt.visualization.client.visualizations.corechart.Options;

public class Goals extends VerticalPanel		{
	private static final Logger log = Logger.getLogger(Goals.class.getName());
	private final GoalsTable goalsTable = new GoalsTable();
	private final AssetTrackingServiceAsync assetTrackingService = GWT
		.create(AssetTrackingService.class);
	private boolean firstRun = true;
	
	public Goals()			{
	}
	
	public void update()	{
		if (firstRun)	{
			initialise();
		} else {
			refresh();
		}
	}
	
	public void initialise()	{
		firstRun = false;
		add(goalsTable);

		/*
		Runnable onLoadCallback = new Runnable() {
	      public void run() {
	    	  setHeight("600px");
	    	  setWidth("600px");
	    	  add(getWidget());
	      }
	    };
	    VisualizationUtils.loadVisualizationApi(onLoadCallback, BarChart.PACKAGE);		*/
		
		goalsTable.setGoals(this);
		try {
			assetTrackingService.retrieveGoals(new AsyncCallback<List<FinanceGoalDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("Transactions: " + caught.toString());
					}

					@Override
					public void onSuccess(List<FinanceGoalDTO> result) {
						goalsTable.update(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("Transactions: " + e.toString());
		} 
	}
	public void refresh()	{
		
	}
	public void insertNewGoal()	{
		try {
			assetTrackingService.insertGoals(new AsyncCallback<Boolean>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("Goals: " + caught.toString());
					}

					@Override
					public void onSuccess(Boolean result) {

					}
				});
		} catch (IllegalArgumentException e) {
			log.info("Goals: " + e.toString());
		} 
		
	}
	
	/*
	  public Widget getWidget() {
		    VerticalPanel result = new VerticalPanel();
		    Options options = Options.create();
		    options.setHeight(240);
		    options.setTitle("Company Performance");
		    options.setWidth(400);
		    AxisOptions vAxisOptions = AxisOptions.create();
		    vAxisOptions.setMinValue(0);
		    vAxisOptions.setMaxValue(2000);
		    options.setVAxisOptions(vAxisOptions);

		    DataTable data = createTable();
		    BarChart viz = new BarChart(data, options);

		    Label status = new Label();
		    Label onMouseOverAndOutStatus = new Label();
//		    viz.addSelectHandler(new SelectionDemo(viz, status));
//		    viz.addReadyHandler(new ReadyDemo(status));
//		    viz.addOnMouseOverHandler(new OnMouseOverDemo(onMouseOverAndOutStatus));
//		    viz.addOnMouseOutHandler(new OnMouseOutDemo(onMouseOverAndOutStatus));
//		    result.add(status);
//		    result.add(viz);
//		    result.add(onMouseOverAndOutStatus);
		    return result;
		  }	
	  
	  private DataTable createTable() {
		  	List<Double> items = new ArrayList<Double>();
		  	items.add(12.0);
		  	items.add(13.0);
		  	items.add(14.0);
		  	
		  	List<String> axis = new ArrayList<String>();
		  	axis.add("Abc");
		  	axis.add("def");
		  	axis.add("ghi");
		  	
		    DataTable data = DataTable.create();
		    data.addColumn(ColumnType.STRING, "Name");
		    data.addColumn(ColumnType.NUMBER, "Number");
		    data.addRows(items.size());
		    int count = 0;
		    for (Double item:items)	{
		    	data.setValue(count, 0, axis.get(count));
		        data.setValue(count, 1, item);
		        count++;
		    }
	    return data;
	  }		*/
	  
}
